use List::Util qw{ sum };
use Test::More tests => 6;

BEGIN { use_ok('Batch::Cluster') };

#########################

SKIP: {
  skip 'Developer test', 5 if !defined $ENV{DEVELOPER};

  my @hostnames = qw{banot tonga};

  # Prueba la conexión con todas las máquinas.
  {
    my $c = Batch::Cluster->new(@hostnames);
    my @list = $c->list_machines();
    is_deeply(\@list, \@hostnames, "Connecting to all machines");
  }

  # Prueba la ejecución en contexto escalar.
  {
    my $c = Batch::Cluster->new(@hostnames);
    my $res = $c->run("echo 'hola'", "echo 'mundo'");
    my $expected = bless( {
                   'results' => [
                                  bless( {
                                           'stderr' => '',
                                           'errmsg' => '',
                                           'errcode' => 0,
                                           'stdout' => '',
                                           'results' => [
                                                          'hola'
                                                        ]
                                         }, 'GRID::Machine::Result' ),
                                  bless( {
                                           'stderr' => '',
                                           'errmsg' => '',
                                           'errcode' => 0,
                                           'stdout' => '',
                                           'results' => [
                                                          'mundo'
                                                        ]
                                         }, 'GRID::Machine::Result' )
                                ]
                 }, 'Batch::Cluster::Result' );
    is_deeply($res, $expected, "Running in scalar context");
  }

  # Prueba la ejecución en contexto de lista.
  {
    my $c = Batch::Cluster->new(@hostnames);
    my @res = $c->run("echo 'hola'", "echo 'mundo'");
    my @expected = ('hola', 'mundo');
    is(@res, @expected, "Running in list context");
  }

  # Prueba el cambio de directorio.
  {
    my $c = Batch::Cluster->new(@hostnames);
    $c->chdir("/tmp");
    my @res = $c->run("pwd", "pwd");
    my @expected = ('/tmp', '/tmp');
    is(@res, @expected, "Changing directory");
  }

  # Realiza el cálculo paralelo de Pi.
  {
    my $np = 5;    
    my $N = 50000;   
    my $clean = 1;  
    my $c = Batch::Cluster->new(@hostnames) || die "Error Intializing Cluster";
    # Transference of files to remote hosts
    $c->copyandmake(
      dir => 'pi',
      makeargs => 'pi',
      files => [ qw{etc/pi/pi.c etc/pi/Makefile} ],
      cleanfiles => $clean,
      cleandirs => $clean, # remove the whole directory at the end
      keepdir => 1,
    );


    $c->chdir("pi/")  || die "Can't change to pi/\n";

    my @commands = map {  "./pi $_ $N $np" } 0..$np-1;

    my $pi = sum $c->run(@commands);

    ok(($pi > 3.1) && ($pi < 3.2), "Parallel Pi Calculation");
  }

}
